import Vue from 'vue'
// import store from '@/store'

// 样式
Vue.directive('body-style', {
	inserted: function(el, binding) {
		const styleBody = { ...binding.value.styleBody }
		for (const key in styleBody) {
			// eslint-disable-next-line no-prototype-builtins
			if (styleBody.hasOwnProperty(key)) {
				el.style[key] = styleBody[key]
			}
		}
	}
})

// 锚点定位
Vue.directive('anchor', {
	inserted: function(el, binding) {
		el.onclick = function() {
			let total
			if (binding.value == 0) {
				total = 0
			} else {
				total = document.getElementById(`anchor-${binding.value}`).offsetTop
			}
			let distance = document.documentElement.scrollTop || document.body.scrollTop
			let step = total / 50
			if (total > distance) {
				;(function smoothDown() {
					if (distance < total) {
						distance += step
						document.documentElement.scrollTop = distance
						setTimeout(smoothDown, 5)
					} else {
						document.documentElement.scrollTop = total
					}
				})()
			} else {
				let newTotal = distance - total
				step = newTotal / 50
				;(function smoothUp() {
					if (distance > total) {
						distance -= step
						document.documentElement.scrollTop = distance
						setTimeout(smoothUp, 5)
					} else {
						document.documentElement.scrollTop = total
					}
				})()
			}
		}
	}
})
